require('es6-promise').polyfill();

import axios from 'axios'

function createSysRoleData(data){
  return {
    type:'CREATE_SYSROLE_DATA',
    data,
    date:Date.now()
  };
}

function errorAction(error) {
  return {
    type: 'ERROR_SYSROLE',
    error
  }
}

export function createSysRoleDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysrole/create',data).then(
      resp => dispatch(loadSysRoleDataAsync({})),
      error => dispatch(errorAction(error))
    );
  };
}


function editSysRoleData(data){
  return {
    type:'EDIT_SYSROLE_DATA',
    data,
    date:Date.now()
  };
}

export function editSysRoleDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysrole/update',data).then(
      resp => dispatch(loadSysRoleDataAsync({})),
      error => dispatch(errorAction(error))
    );
  };
}

//根据role_id获取sysusers
export function SysRoleQueryAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysrole/querySysUsers',data).then(
      resp => dispatch(loadSysRoleUserData(resp.data)),
      error => dispatch(errorAction(error))
    );
  };
}


function deleteSysRoleData(id) {
  return {
    type: 'DELETE_SYSROLE_DATA',
    id
  };
}

export function deleteSysRoleDataAsync(id){
  return function(dispatch) {
    return axios.get('/api/sysrole/delete?id='+id).then(
      resp => dispatch(loadSysRoleDataAsync({})),
      error => dispatch(errorAction(error))
    );
  };
}



function loadSysRoleData(data) {
  return {
    type: 'LOAD_SYSROLE_DATA',
    data
  }
}

function loadSysRoleUserData(data) {
  return {
    type: 'LOAD_SYSUSER_DATA',
    data
  }
}

export function loadSysRoleDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysrole',data).then(
      resp => dispatch(loadSysRoleData(resp.data)),
      error => dispatch(errorAction(error))
    );
  };
}
